package Offer2017;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 两个队列实现栈
 * Created by 雷文康 on 2017/12/28.
 */
public class Test07a {
    Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();
    public void add(int value){
        if (!queue1.isEmpty()) {
            queue1.add(value);
        } else if (!queue2.isEmpty()){
            queue2.add(value);
        } else {
            queue1.add(value);
        }
    }

    public int pop() {
        if (queue1.isEmpty() && !queue2.isEmpty()) {
            while (queue2.size() > 1) {
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        } else if (!queue1.isEmpty() && queue2.isEmpty()){
            while (queue1.size() > 1) {
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }
        return -1;
    }
    public boolean isNotEmpty(){
        return !queue1.isEmpty() || !queue2.isEmpty();
    }

    public static void main(String[] args) {
        Test07a stack = new Test07a();
        stack.add(1);
        stack.add(2);
        stack.add(3);
        stack.add(4);
        stack.add(5);
        while (stack.isNotEmpty()){
            System.out.println( stack.pop());
        }
    }
}
